<template>
  <div class="app-container">
    <Find :OriginalFilter="{ static: true }" :formList="formList" @findClicked="findClicked" />

    <div class="mt10">
      <el-card shadow="never" :body-style="{ padding: '10px 10px 0px' }" style="flex-shrink: 0">
        <div slot="header" class="f16">
          <span style="font-weight: 600">补料单资料</span>
        </div>
        <Table
          :tableData="tableData"
          :tableHeader="tableHeader"
          :isLoading="isLoading"
          @currentRow="currentRow"
          :TableHeight="33.3"
          :outTableHeight="185"
        />
      </el-card>

      <el-card shadow="never" :body-style="{ padding: '0px' }" class="main">
        <el-card shadow="never" :body-style="{ padding: '10px 10px 0px' }">
          <div slot="header" class="mb9 f16 flex" style="align-items: center">
            <span style="font-weight: 600" class="mr24">补制明细</span>
            <div class="find f14">
              <div class="flex" style="align-items: center">
                <span class="mr10">客人号</span>
                <Find :OriginalFilter="{ static: true }" :formList="formList2" :searchImg="false" />
              </div>
              <div class="flex" style="align-items: center">
                <span class="mr10">大底号</span>
                <Find :OriginalFilter="{ static: true }" :formList="formList3" :searchImg="false" />
              </div>
            </div>
          </div>
          <Table :tableData="tableData2" :tableHeader="tableHeader2" :TableHeight="33.3" :outTableHeight="185" />
        </el-card>
        <el-card shadow="never" :body-style="{ padding: '10px 10px 10px' }">
          <div slot="header" class="mb9 f16">
            <span style="font-weight: 600">补料明细</span>
          </div>
          <div class="flex">
            <Table
              :tableData="tableData3"
              :tableHeader="tableHeader3"
              :TableHeight="33.3"
              :outTableHeight="185"
              style="margin-right: 30px"
              @currentRow="currentRow2"
            />
            <Table :tableData="tableData4" :tableHeader="tableHeader4" :TableHeight="33.3" :outTableHeight="185" style="margin-right: 30px" />
            <Table :tableData="tableData5" :tableHeader="tableHeader5" :TableHeight="33.3" :outTableHeight="185" />
          </div>
          <PageModule :PageData="{ total }" @pageSizeChange="handleSizeChange" @pageChange="handleCurrentChange" />
        </el-card>
      </el-card>
    </div>
  </div>
</template>
<script lang="ts">
import { selectList } from '@/plugins/mixin'
import service from '@/utils/request'
import { ElMessage } from 'element-plus'
import { debounce } from 'lodash'
import { defineComponent, onMounted, ref, watch } from 'vue'
import JMessage from '@/utils/JMessage'
export default defineComponent({
  name: 'FillOrderQueryJobKa'
})
</script>
<script lang="ts" setup>
import { i18n, Jzhcn } from '@/i18n'
const ChargeBackVal = ref('') // 退单说明
const imgList = ref([])
const formList = ref([
  {
    type: 'input',
    inputPlaceholder: '请输入补料单号',
    inputValue: ''
  },
  {
    type: 'input',
    inputPlaceholder: '请输入指令',
    inputValue: ''
  },
  {
    type: 'select',
    selectValue: '',
    selectP: '补料部门',
    selectOptions: []
  },
  {
    type: 'select',
    selectValue: '',
    selectP: '补料类型',
    selectOptions: []
  },
  {
    type: 'date',
    dateLabel: '补料日期',
    dateValue: ''
  },
  {
    type: 'checkbox',
    checkboxLabel: '转审核',
    checked: false
  },
  {
    type: 'checkbox',
    checkboxLabel: '未审核',
    checked: true
  },
  {
    type: 'checkbox',
    checkboxLabel: '已审核',
    checked: true
  },
  {
    type: 'checkbox',
    checkboxLabel: '已打印',
    checked: false
  },
  {
    type: 'button',
    checkboxLabel: '置空',
    bgColor: '#1890FF'
  },
  {
    type: 'button',
    checkboxLabel: '打印补料单(14个号码)'
  },
  {
    type: 'button',
    checkboxLabel: '打印补料单'
  }
])
const tableHeader = ref([
  {
    label: '补料单号',
    prop: 'slipNo'
  },
  {
    label: '补料日期',
    prop: 'slipDate'
  },
  {
    label: '补料部门',
    prop: 'dutyDeptName'
  },
  {
    type: 'checkbox',
    width: '50',
    label: '打印',
    prop: 'prnFlag'
  },
  {
    type: 'checkbox',
    label: '转审核',
    prop: 'cfmFlag',
    width: '50'
  },
  {
    type: 'checkbox',
    label: '审核',
    prop: 'financeFlag',
    width: '50'
  },
  {
    label: '补料类型',
    prop: 'blFlag'
  },
  {
    label: '备注说明',
    prop: 'dutyMemo',
    width: '250'
  },
  {
    label: '退单说明',
    prop: 'returnRek'
  },
  {
    type: 'img',
    label: '图片',
    fixed: true,
    prop: 'photoPath',
    srcList: imgList
  }
])

// 表一模块
// '置空'按钮
const findClicked = (content: string) => {
  if (content === '置空') {
    formList.value.forEach((item: any) => {
      item.inputValue = ''
      item.selectValue = ''
      item.dateValue = ''
    })
  }
  getTableList()
}
const tableData = ref<any>([])
const isLoading = ref(true)
// 分页器
const currentPage = ref(1)
const pageSize = ref(40)
const total = ref(0)
const handleSizeChange = (val: number) => {
  pageSize.value = val
  getTableList()
}
const handleCurrentChange = (val: number) => {
  currentPage.value = val
  getTableList()
}
// 表格计算函数
const cfmFlag = () => {
  const Unaudited = formList.value.find((item: any) => item.type === 'checkbox' && i18n.global.t(item.checkboxLabel) === '已审核')
  const Audited = formList.value.find((item: any) => item.type === 'checkbox' && i18n.global.t(item.checkboxLabel) === '未审核')
  if (!Unaudited || !Audited) return
  if (Unaudited.checked && Audited.checked) {
    return ''
  } else if (Unaudited.checked) {
    return '1'
  } else if (Audited.checked) {
    return '2'
  } else {
    ElMessage('必须至少选择一项')
    Unaudited.checked = true
    Audited.checked = true
    return ''
  }
}
// 获取表数据
const getTableList = debounce(async () => {
  isLoading.value = true
  let tableDataList = await service.post<any, any>('/pcFactorWork/getSupplementaryOrder', {
    blFlag: formList.value.find((item: any) => item.selectP && Jzhcn.Find[item.selectP.split('.')[1] as keyof typeof Jzhcn.Find] === '补料类型')
      ?.selectValue,
    cfmFlag: formList.value.find((item: any) => item.checkboxLabel === '转审核')?.checked ? 'Y' : '',
    dutyDeptName: formList.value.find((item: any) => item.selectP && Jzhcn.Find[item.selectP.split('.')[1] as keyof typeof Jzhcn.Find] === '补料部门')
      ?.selectValue,
    endslipDate: formList.value.find(
      (item: any) => item.dateLabel && Jzhcn.Find[item.dateLabel.split('.')[1] as keyof typeof Jzhcn.Find] === '补料日期'
    )?.dateValue?.[1],
    financeFlag: cfmFlag(),
    lotNo: formList.value.find(
      (item: any) => item.inputPlaceholder && Jzhcn.Find[item.inputPlaceholder.split('.')[1] as keyof typeof Jzhcn.Find] === '请输入指令'
    )?.inputValue,
    pageNum: currentPage.value,
    pageSize: pageSize.value,
    prnFlag: formList.value.find((item: any) => item.checkboxLabel === '转审核')?.checked ? 'Y' : '',
    slipNo: formList.value.find(
      (item: any) => item.inputPlaceholder && Jzhcn.Find[item.inputPlaceholder.split('.')[1] as keyof typeof Jzhcn.Find] === '请输入补料单号'
    )?.inputValue,
    startslipDate: formList.value.find(
      (item: any) => item.dateLabel && Jzhcn.Find[item.dateLabel.split('.')[1] as keyof typeof Jzhcn.Find] === '补料日期'
    )?.dateValue?.[0]
  })
  isLoading.value = false
  imgList.value = tableDataList.supplementaryOrderInfo.map((item: any) => {
    return item.photoPath
  })
  let NewTableData = tableDataList.supplementaryOrderInfo.map((item: any) => {
    item.prnFlag === 'Y' ? (item.prnFlag = true) : (item.prnFlag = false)
    item.cfmFlag === 'Y' ? (item.cfmFlag = true) : (item.cfmFlag = false)
    item.financeFlag === 'Y' ? (item.financeFlag = true) : (item.financeFlag = false)
    return item
  })
  tableData.value = NewTableData
  total.value = tableDataList.total
  currentRow()
}, 500)
onMounted(() => {
  getTableList()
})
watch(
  () =>
    formList.value.map((item: any) => {
      item.selectValue, item.inputValue, item.dateValue, item.checked
    }),
  () => {
    getTableList()
  }
)
const currentSlipNo = ref('')
// 点击表给另一张表传数据
const currentRow = async (row: any = tableData.value[0] ? tableData.value[0] : {}) => {
  let { slipNo }: any = row
  currentSlipNo.value = slipNo
  // 给表二传指令号
  let table2Data = await service.get<any, any>(`/pcFactorWork/getSupplementaryDetail`, {
    params: {
      slipNo
    }
  })
  tableData2.value = table2Data.supplementaryInfo
  tableData3.value = table2Data.materialDetail
  formList2.value = []
  formList3.value = []
  table2Data.numberDetail.forEach((item: any) => {
    formList2.value.push({
      type: 'input',
      inputPlaceholder: '',
      inputDisabled: true,
      inputValue: item.sizeNo,
      inputLabel: ''
    })
    formList3.value.push({
      type: 'input',
      inputPlaceholder: '',
      inputDisabled: true,
      inputValue: item.soleSize,
      inputLabel: ''
    })
  })
}

const tableHeader2 = ref([
  {
    label: '指令',
    prop: 'lotNo',
    width: '109',
    fixed: 'left'
  },
  {
    label: '型体',
    prop: 'style',
    width: '109',
    fixed: 'left'
  },
  {
    label: '指令数',
    prop: 'ordQty',
    width: '80',
    fixed: 'left'
  },
  {
    label: '片数',
    prop: 'cutNum',
    width: '80',
    fixed: 'left'
  },
  {
    label: '部位',
    prop: 'partName',
    width: '80',
    fixed: 'left'
  },
  {
    label: '段',
    prop: 'segment',
    width: '45',
    fixed: 'left'
  },
  {
    label: '预补双数',
    prop: 'mUsage',
    width: '80',
    fixed: 'left'
  },
  {
    label: '左',
    prop: 'lqty1',
    width: '73'
  },
  {
    label: '右',
    prop: 'rqty1',
    width: '73'
  },
  {
    label: '左',
    prop: 'lqty2',
    width: '73'
  },
  {
    label: '右',
    prop: 'rqty2',
    width: '73'
  },
  {
    label: '左',
    prop: 'lqty3',
    width: '73'
  },
  {
    label: '右',
    prop: 'rqty3',
    width: '73'
  },
  {
    label: '左',
    prop: 'lqty4',
    width: '73'
  },
  {
    label: '右',
    prop: 'rqty4',
    width: '73'
  },
  {
    label: '左',
    prop: 'lqty5',
    width: '73'
  },
  {
    label: '右',
    prop: 'rqty5',
    width: '73'
  },
  {
    label: '左',
    prop: 'lqty6',
    width: '73'
  },
  {
    label: '右',
    prop: 'rqty6',
    width: '73'
  },
  {
    label: '左',
    prop: 'lqty7',
    width: '73'
  },
  {
    label: '右',
    prop: 'rqty7',
    width: '73'
  },
  {
    label: '左',
    prop: 'lqty8',
    width: '73'
  },
  {
    label: '右',
    prop: 'rqty8',
    width: '73'
  },
  {
    label: '左',
    prop: 'lqty9',
    width: '73'
  },
  {
    label: '右',
    prop: 'rqty9',
    width: '73'
  },
  {
    label: '左',
    prop: 'lqty10',
    width: '73'
  },
  {
    label: '右',
    prop: 'rqty10',
    width: '73'
  },
  {
    label: '左',
    prop: 'lqty11',
    width: '73'
  },
  {
    label: '右',
    prop: 'rqty11',
    width: '73'
  },
  {
    label: '左',
    prop: 'lqty12',
    width: '73'
  },
  {
    label: '右',
    prop: 'rqty12',
    width: '73'
  },
  {
    label: '左',
    prop: 'lqty13',
    width: '73'
  },
  {
    label: '右',
    prop: 'rqty13',
    width: '73'
  },
  {
    label: '左',
    prop: 'lqty14',
    width: '73'
  },
  {
    label: '右',
    prop: 'rqty14',
    width: '73'
  },
  {
    label: '左',
    prop: 'lqty15',
    width: '73'
  },
  {
    label: '右',
    prop: 'rqty15',
    width: '73'
  },
  {
    label: '左',
    prop: 'lqty16',
    width: '73'
  },
  {
    label: '右',
    prop: 'rqty16',
    width: '73'
  },
  {
    label: '左',
    prop: 'lqty17',
    width: '73'
  },
  {
    label: '右',
    prop: 'rqty17',
    width: '73'
  },
  {
    label: '左',
    prop: 'lqty18',
    width: '73'
  },
  {
    label: '右',
    prop: 'rqty18',
    width: '73'
  },
  {
    label: '左',
    prop: 'lqty19',
    width: '73'
  },
  {
    label: '右',
    prop: 'rqty19',
    width: '73'
  },
  {
    label: '左',
    prop: 'lqty20',
    width: '73'
  },
  {
    label: '右',
    prop: 'rqty20',
    width: '73'
  },

  {
    label: '尺码起',
    prop: 'size1',
    width: '73'
  },
  {
    label: '尺码止',
    prop: 'size2',
    width: '73'
  },
  {
    label: '物料编号',
    prop: 'mtlNo',
    fixed: 'right',
    width: '123'
  },
  {
    label: '合计（双）',
    prop: 'totalQty',
    fixed: 'right',
    width: '73'
  },
  {
    label: '补制比例%',
    prop: 'percent',
    fixed: 'right',
    width: '73'
  }
])
const tableData2 = ref([])

const tableHeader3 = ref([
  {
    label: '物料编码',
    prop: 'mtlNo',
    width: '130'
  },
  {
    label: '材料颜色',
    prop: 'mtlColor',
    width: '140'
  },
  {
    label: '材料名称',
    prop: 'mtlName',
    width: '175'
  },
  {
    label: '规格',
    prop: 'mtlSpec',
    width: '78'
  },
  {
    label: '单位',
    prop: 'mtlUnit',
    width: '50'
  },
  {
    label: '需求用量',
    prop: 'reqQty',
    width: '75 '
  },
  {
    label: '原仓应发量',
    prop: 'actQty',
    width: '79'
  },
  {
    label: '在用库存',
    prop: 'subQty',
    width: '82'
  },
  {
    label: '责任用量',
    prop: 'actQty1',
    width: '73'
  }
])
const tableData3 = ref([])
const currentRow2 = async (row: any) => {
  let { mtlNo } = row
  let table4Data = await service.put<any, any>(`/pcFactorWork/listMtlLot`, {
    slipNo: currentSlipNo.value,
    mtlNo
  })
  tableData4.value = table4Data.lot
  tableData5.value = table4Data.duty
}

const tableHeader4 = ref([
  {
    label: '指令',
    prop: 'lotNo',
    width: '120'
  },
  {
    label: '数量',
    prop: 'qty',
    width: '55'
  }
])
const tableData4 = ref([])

const tableHeader5 = ref([
  {
    label: '责任厂商',
    prop: 'dutyQty',
    width: '75'
  },
  {
    label: '责任员工',
    prop: 'dutyName2',
    width: '75'
  },
  {
    label: '数量',
    prop: 'dutyName',
    width: '55'
  }
])
const tableData5 = ref([])

const formList2 = ref<any>([])
const formList3 = ref<any>([])

const getSelectList = async () => {
  await selectList(formList, () => service.get('/pcFactorWork/getDept2Type'))
}
onMounted(() => {
  getSelectList()
})
</script>

<style lang="less" scoped>
.find {
  :deep(.form .el-form-item) {
    margin: 5px 2px 0 0 !important;
    margin-right: 10px !important;
  }

  :deep(.disable) {
    width: 65px !important;
  }
}
</style>
